package com.youlu.campus.web.vote.controller;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.course.group.Info;
import com.youlu.campus.entity.vote.dto.*;
import com.youlu.campus.web.vote.service.ActivityVoteHomeService;
import com.youlu.campus.web.config.aspect.VotePvStatistics;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 投票活动首页控制器
 * 处理投票活动相关的排行榜查询、用户投票及用户信息保存等操作
 *
 * @author : [dengbo]
 * @createTime : [2022/6/30 10:44]
 */
@Slf4j
@RestController
@RequestMapping("/mini-app/")
public class ActivityVoteHomeController {
    @Autowired
    private ActivityVoteHomeService activityVoteHomeService;

    /**
     * 组织投票排行榜接口
     * 组织可能是学校、班级、单位、机构等，支持分页查询排行榜信息
     * @param request 投票首页查询DTO，包含活动ID等筛选条件
     * @return 组织投票排行榜数据
     */
    @PostMapping("/org/vote/rank")
    @VotePvStatistics(activityId = "#request.activityId")
    public QResult voteRankSearch(@Validated(Info.class) @RequestBody VoteHomeSearchDTO request) {
        String userId = RequestContext.getUserId();
        log.info("ActivityVoteHomeController,voteRankSearch,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setUserPO(RequestContext.getCurUser());
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(activityVoteHomeService.getOrgVoteRankPage(request));
    }

    /**
     * 用户投票排行榜接口
     * 分页查询参与投票活动的用户排行榜信息
     * @param request 用户投票查询DTO，包含活动ID等筛选条件
     * @return 用户投票排行榜数据
     */
    @PostMapping("/user/vote/rank")
    public QResult getUserRank(@Validated(Info.class) @RequestBody UserVoteSearchDTO request) {
        String userId = RequestContext.getUserId();
        log.info("ActivityVoteHomeController,userRank,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        return QResult.success(activityVoteHomeService.getUserVoteRankPage(request));
    }

    /**
     * 用户投票接口
     * 处理用户对组织的投票操作，记录投票信息
     * @param request 投票操作DTO，包含投票对象ID等信息
     * @return 投票操作结果
     */
    @PostMapping("/user/org/vote")
    public QResult userOrgVote(@Validated(Info.class) @RequestBody VotePointOpDTO request) {
        String userId = RequestContext.getUserId();
        log.info("ActivityVoteHomeController,userOrgVote,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        return QResult.success(activityVoteHomeService.userOrgVote(request));
    }

    /**
     * 用户投票活动基本信息保存接口
     * 保存用户参与投票活动的基本信息（如个人资料等）
     * @param request 用户基本信息保存DTO，包含用户相关信息
     * @return 信息保存结果
     */
    @PostMapping("/user/base/info/save")
    public QResult saveUserInfo(@Validated(Info.class) @RequestBody UserBaseInfoSaveDTO request) {
        String userId = RequestContext.getUserId();
        log.info("ActivityVoteHomeController,saveUserInfo,userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        return QResult.success(activityVoteHomeService.saveUserInfo(request));
    }
}